import { rejects } from 'assert';
import { connection } from '../connection'

/**
 * 注册
 * @param {object} obj
 */
const register = (obj: {
    userName: string,
    passWord: string,
    nickName: string,
    acatar: string,
    admin: number,
    createTime: number,
    updataTime: number
}) => {
    return new Promise((resolve, reject) => {
        const { userName, passWord, acatar, nickName, admin, createTime, updataTime } = obj;
        connection.query('insert into user_info (userName,passWord,acatar,nickName,admin,createTime,updataTime) values (?,?,?,?,?,?,?)',
            [userName, passWord, acatar, nickName, admin, createTime, updataTime],
            (err, results) => {
                if (!err) {
                    resolve(results)
                } else {
                    reject(results)
                }
            }
        );
    })
}

/**
 * 查询是否存在用户数据
 * @param {string} userName
 * @returns {boolean}
 */
const paramsquery = (userName: string) => {
    return new Promise((resolve, reject) => {
        connection.query('select * from user_info where userName = ?', [userName], (err, results) => {
            if (err) {
                reject({ status: 400, desc: '查询失败' })
            }
            resolve({ status: 200, desc: '查询成功', data: results })
        })
    })
}

/**
 * 更新用户信息
 * @param {string} userName
 * @param {object} obj
 */
const updataUserInfo = (userName: string,obj: {
    nickName: string,
    mobile: number,
    province: string,
    city: string,
    zone: string,
    address: string,
    updataTime: number,
    acatar: string,
 }) => {
    return new Promise((resolve,reject) => {
        const {
            nickName,
            mobile,
            province,
            city,
            zone,
            address,
            updataTime,
            acatar,
        } = obj
        connection.query('update user_info set nickName=?,mobile=?,province=?,city=?,zone=?,address=?,updataTime=?,acatar=? where userName = ?',
            [nickName,mobile,province,city,zone,address,updataTime,acatar,userName],
            (err, results) => {
                if (!err) {
                    resolve(results)
                } else {
                    reject(results)
                }
            }
        )
    })
};

export {
    paramsquery,
    register,
    updataUserInfo
}